package com.ww.java8.fp.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * Function<T, R>使用示例
 */
public class LambdaDemo4 {

    public static void main(String[] args) {
        List<Integer> l = map(Arrays.asList("lambdas", "in", "action"), s -> s.length());
        for (Integer len : l) {
            System.out.println("字符串长度：" + len);
        }
    }

    /**
     * 通过字符串计算长度
     * @param list
     * @param f
     * @return
     * @param <T>
     * @param <R>
     */
    public static <T, R> List<R> map(List<T> list, Function<T, R> f) {
        List<R> result = new ArrayList<>();
        for (T t : list) {
            result.add(f.apply(t));
        }
        return result;
    }
}
